package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.li.mapper.OrderSettingMapper;
import com.li.pojo.OrderSetting;
import com.li.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Override
    public void add(List<OrderSetting> orderSettings) {
        //判断预约设置是否为空
        if(orderSettings!=null&&orderSettings.size()>0){
            for (OrderSetting orderSetting : orderSettings) {

                //检查日期是否存在
                Long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    //已经存在执行更新
                    orderSettingMapper.updateOrderSetting(orderSetting);
                }else{
                    //不存在 执行插入
                    orderSettingMapper.Add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {

        String dateBegin=date+"-1";
        String dateEnd=date+"-31";

        HashMap map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);

        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);

        ArrayList<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {

            HashMap orderSettingMap = new HashMap();

            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }

        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        Long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());

        if(count>0){
            //执行更新
            orderSettingMapper.editNumberByOrderDate(orderSetting);

        }else{
            //执行插入
            orderSettingMapper.Add(orderSetting);
        }
    }

    @Override
    public void delete4Month(String date) {
        orderSettingMapper.delete4Month(date);
    }
}
